/**
 * 
 */
package switchviz.gui;

import java.awt.Desktop;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import org.apache.log4j.Logger;

import switchviz.Application;
import switchviz.core.ResourceManager;


/**
 * @author Thomas
 *
 */
public class MainFrame extends JFrame
{
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(MainFrame.class);
	
	private MainPanel _mainPanel 		= null;
	private MenuBar _menuBar 			= null;
	
	private AboutDialog _aboutDialog 	= null;
	
	/**
	 * This is the default constructor
	 */
	public MainFrame()
	{
		super();
		if (logger.isInfoEnabled()) { logger.info("Initialize ..."); }
		initialize();
		this.setIconImage(ResourceManager.getInstance().getImageIconSV().getImage());
	}

	public MainPanel getMainPanel()
	{
		if (_mainPanel == null)
			_mainPanel = new MainPanel();
		
		return _mainPanel;
	}
	
	private void initialize()
	{
		this.setSize(1024, 768);
		this.setTitle("SwitchViz " + Application.getVersion());
		this.setContentPane(getMainPanel());
		this.setJMenuBar(getMainMenuBar());
		this.addWindowListener(new WindowAdapter() 
		{
	        public void windowClosing(WindowEvent e) 
	        {
		          new Thread(new Runnable() 
		          {
		              public void run() 
		              {
		                System.exit(0);
		              }
		          }).start();
		    }
		});
		logger.debug("Initialized ...");
	}
	
	private void initializeMenuBar()
	{
		_menuBar.getExitMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		_menuBar.getAboutMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JDialog aboutDialog = getAboutDialog();
				aboutDialog.pack();
				Point loc = getLocation();
				loc.translate(20, 20);
				aboutDialog.setLocation(loc);
				aboutDialog.setVisible(true);
			}
		});
		_menuBar.getImportViewMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new ImportDialog(ImportDialog.VIEWIMPORT);
			}
		});
		_menuBar.getImportRecordMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new ImportDialog(ImportDialog.RECORDIMPORT);
			}
		});
		_menuBar.getExportViewMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new ExportDialog(Application.getInstance().getMainFrame(), ExportDialog.EXPORTVIEW);
			}
		});
		_menuBar.getExportRecordMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new ExportDialog(Application.getInstance().getMainFrame(), ExportDialog.EXPORTRECORD);
			}
		});
		_menuBar.getUserManualMenuItem().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				openUserManual();
			}
		});
	}

	private MenuBar getMainMenuBar()
	{
		if (_menuBar == null)
		{
			_menuBar = new MenuBar();
			
			initializeMenuBar();
		}
		
		return _menuBar;
	}

	private AboutDialog getAboutDialog()
	{
		if (_aboutDialog == null)
			_aboutDialog = new AboutDialog(this);
		
		return _aboutDialog;
	}
	
	private void openUserManual()
	{
		try 
		{
			Desktop.getDesktop().open(ResourceManager.getInstance().getUserManualFile());
		} catch (IOException e) 
		{
			logger.error("Can't open UserManual: " + e);
		} 
	}
}
